using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Design;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.Util;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Web.UI.Design.WebControls;

namespace PickGold.Controls
{
	[SupportsPreviewControl(true), SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
	public class InputBoxDesigner : DataBoundControlDesigner
	{
		protected override string GetErrorDesignTimeHtml(Exception e)
		{
			return string.Format("<h1 style=\"color: red;\">Error: {0}</h1><hr />", e.Message)
				+ e.StackTrace.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br />");
			//return base.GetErrorDesignTimeHtml(e);
		}

		internal void ConnectToDataSourceAction()
		{
			ControlDesigner.InvokeTransactedChange(base.Component, new TransactedChangeCallback(this.ConnectToDataSourceCallback), null, "List Control Designer Connect To Data Source");
		}

		private bool ConnectToDataSourceCallback(object context)
		{
			//ListControlConnectToDataSourceDialog form = new ListControlConnectToDataSourceDialog(this);
			//return (UIServiceHelper.ShowDialog(base.Component.Site, form) == DialogResult.OK);
			return false;
		}

		protected override void DataBind(BaseDataBoundControl dataBoundControl)
		{
		}

		internal void EditItems()
		{
			PropertyDescriptor context = TypeDescriptor.GetProperties(base.Component)["Items"];
			ControlDesigner.InvokeTransactedChange(base.Component, new TransactedChangeCallback(this.EditItemsCallback), context, "List Control Designer Edit Items", context);
		}

		private bool EditItemsCallback(object context)
		{
			//IDesignerHost service = (IDesignerHost) this.GetService(typeof(IDesignerHost));
			//PropertyDescriptor propDesc = (PropertyDescriptor) context;
			//new ListItemsCollectionEditor(typeof(ListItemCollection)).EditValue(new System.Web.UI.Design.WebControls.TypeDescriptorContext(service, propDesc, base.Component), new WindowsFormsEditorServiceHelper(this), propDesc.GetValue(base.Component));
			//return true;
			return false;
		}

		public override string GetDesignTimeHtml()
		{
			InputBox viewControl = (InputBox)base.ViewControl;
			InputBoxType type = viewControl.InputType;
			if (type == InputBoxType.Select || type == InputBoxType.RadioList || type == InputBoxType.CheckList)
			{
				try
				{
					ListItemCollection items = viewControl.Items;
					bool flag = this.IsDataBound();
					if (items.Count == 0 || flag)
					{
						if (flag)
						{
							items.Clear();
							items.Add("Sample Databound Text");
						}
						else
						{
							items.Add("Sample Unbound Text");
						}
					}
					return base.GetDesignTimeHtml();
				}
				catch (Exception x)
				{
					Console.WriteLine(x);
					return this.GetErrorDesignTimeHtml(x);
				}
			}

			StringWriter sw = new StringWriter();
			HtmlTextWriter writer = new HtmlTextWriter(sw);
			viewControl.RenderControl(writer);
			string t = sw.ToString();
			//if (string.IsNullOrEmpty(t))
			//	t = "InputBox";
			return t;
			//return base.GetDesignTimeHtml();
		}

		public IEnumerable GetResolvedSelectedDataSource()
		{
			return ((IDataSourceProvider)this).GetResolvedSelectedDataSource();
		}

		public object GetSelectedDataSource()
		{
			return ((IDataSourceProvider)this).GetSelectedDataSource();
		}

		public override void Initialize(IComponent component)
		{
			//ControlDesigner.VerifyInitializeArgument(component, typeof(InputBox));
			base.Initialize(component);
		}
		private bool IsDataBound()
		{
			if (base.DataBindings["DataSource"] == null)
			{
				return (base.DataSourceID.Length > 0);
			}
			return true;
		}

		public virtual void OnDataSourceChanged()
		{
			base.OnDataSourceChanged(true);
		}

		protected override void OnDataSourceChanged(bool forceUpdateView)
		{
			this.OnDataSourceChanged();
		}

		protected override void PreFilterProperties(IDictionary properties)
		{
			base.PreFilterProperties(properties);
			Attribute[] attributes = new Attribute[] { new TypeConverterAttribute(typeof(DataFieldConverter)) };
			PropertyDescriptor oldPropertyDescriptor = (PropertyDescriptor)properties["DataTextField"];
			oldPropertyDescriptor = TypeDescriptor.CreateProperty(base.GetType(), oldPropertyDescriptor, attributes);
			properties["DataTextField"] = oldPropertyDescriptor;
			oldPropertyDescriptor = (PropertyDescriptor)properties["DataValueField"];
			oldPropertyDescriptor = TypeDescriptor.CreateProperty(base.GetType(), oldPropertyDescriptor, attributes);
			properties["DataValueField"] = oldPropertyDescriptor;
		}

		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection lists = new DesignerActionListCollection();
				lists.AddRange(base.ActionLists);
				//lists.Add(new ListControlActionList(this, base.DataSourceDesigner));
				return lists;
			}
		}

		public string DataTextField
		{
			get
			{
				return ((InputBox)base.Component).DataTextField;
			}
			set
			{
				((InputBox)base.Component).DataTextField = value;
			}
		}

		public string DataValueField
		{
			get
			{
				return ((InputBox)base.Component).DataValueField;
			}
			set
			{
				((InputBox)base.Component).DataValueField = value;
			}
		}

		protected override bool UseDataSourcePickerActionList
		{
			get
			{
				return false;
			}
		}
	}
}

